package config_cmd

import (
	"errors"
	"learning/gooop/spring/autogen/command"
	"strings"
)

// SaveASCmd calls service.SaveAS() to save current config into specific file, in JSON format
type SaveASCmd struct {
	file string
}

// SaveASCmdBuilder parse cli input and returns a SaveASCmd instance
type SaveASCmdBuilder int

const gSaveASCmdPrefix = "config saveas "

func (me *SaveASCmd) String() string {
	return gSaveASCmdPrefix + me.file
}

func (me *SaveASCmd) Apply(c command.ICmdContext) error {
	// todo: fixme
	panic("implements me")
}

func (me *SaveASCmdBuilder) Build(line string) (error, command.ICmd) {
	if !strings.HasPrefix(line, gSaveASCmdPrefix) {
		return nil, nil
	}

	file := strings.TrimSpace(line[len(gSaveASCmdPrefix):])
	if len(file) <= 0 {
		return errors.New("empty file path"), nil
	}

	return nil, &SaveASCmd{file}
}
